package com.redistest.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "redisTest")
public class RedisTestController {

	@SuppressWarnings("rawtypes")
	@Autowired
	@Qualifier("slaveRedisTemplate")
	private RedisTemplate readRedistemplate;

	@SuppressWarnings("rawtypes")
	@Autowired
	@Qualifier("masterRedisTemplate")
	private RedisTemplate writeRedistemplate;

	@GetMapping(value = "getKey/{key}")
	public Object getKey(@PathVariable("key") String key) {
		Object data = readRedistemplate.opsForValue().get(key);
		return data;
	}

	@SuppressWarnings("unchecked")
	@GetMapping(value = "setKey/{key}")
	public Object setKey(@PathVariable("key") String key, @RequestParam("value") String value) {
		writeRedistemplate.opsForValue().set(key, value);
		return "OK";
	}

}
